package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.dtos.CollectionBehaviorDTO;
import com.heima.model.behavior.pojos.*;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {

    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    MongoTemplate mongoTemplate;
    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
        //1检验参数  文章ID不能为空  使用注解
        //拿到user对象
        Integer userId = AppThreadLocalUtils.getUser().getId();
        //2调用查询行为实体方法
        //获取设备ID
        Integer equipmentId = dto.getEquipmentId();
        //根据用户ID和设备ID获取当前     实体行为对象
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(userId, equipmentId);
        //3如果是收藏   0 收藏 1 取消收藏
        Query query = Query.query(Criteria.where("articleId").is(dto.getArticleId()).and("entryId").is(apBehaviorEntry.getId()));
        ApUnlikesBehavior apUnlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
        Boolean unlike=true;
        if (apUnlikesBehavior == null) {
            unlike=false;
        }
        ApLikesBehavior apLikesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
        Boolean like=true;
        if (apLikesBehavior == null) {
            like=false;
        }
        ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
        Boolean Collection=true;
        if (apCollection == null) {
            Collection=false;
        }
        HashMap<String, String> map = new HashMap<>();
                 map.put("unlike",unlike.toString());
                 map.put("like",like.toString());
                 map.put("Collection",Collection.toString());
        return ResponseResult.okResult(map);
    }
}
